package com.yun.zf.app.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.app.mapper.ZfHouseBasicMapper;
import com.yun.zf.app.mapper.ZfHouseDescMapper;
import com.yun.zf.app.service.*;
import com.yun.zf.common.admin.dto.community.AgentInfoDto;
import com.yun.zf.common.admin.dto.house.AdminHousePicDto;
import com.yun.zf.common.admin.dto.house.HouseDetailAdminDto;
import com.yun.zf.common.admin.model.house.HouseDescMo;
import com.yun.zf.common.admin.model.house.HouseListMo;
import com.yun.zf.common.app.domain.ZfCommunity;
import com.yun.zf.common.app.domain.ZfHouseBasic;
import com.yun.zf.common.app.dto.detail.CommunityNearByDto;
import com.yun.zf.common.app.dto.detail.HouseDetailInfoDto;
import com.yun.zf.common.app.dto.detail.HouseRentInfoDto;
import com.yun.zf.common.app.dto.detail.HouseSaleInfoDto;
import com.yun.zf.common.app.model.home.RecordHouseBasicInfoMo;
import com.yun.zf.common.mapstruct.app.AppHouseMapper;
import com.yun.zf.system.service.SysAgentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ZfHouseBasicServiceImpl extends ServiceImpl<ZfHouseBasicMapper, ZfHouseBasic> implements ZfHouseBasicService {

    @Autowired
    private ZfHouseDescMapper descMapper;
    @Autowired
    private ZfHousePictureService pictureService;
    @Autowired
    private ZfHouseDetailService houseDetailService;
    @Autowired
    private ZfCommunityService communityService;
    @Autowired
    private ZfFollowService followService;
    @Autowired
    private ZfHouseInfoRentService rentService;
    @Autowired
    private ZfHouseInfoSaleService saleService;
    @Autowired
    private SysAgentService agentService;

    @Override
    public List<HouseListMo> houseBasic(Long provCode, Long cityCode, List<Long> ids) {
        return baseMapper.getRecommondHouse(provCode, cityCode, ids);
    }



    @Override
    public int recordHouseBasic(RecordHouseBasicInfoMo houseBasicInfo) {
       ZfHouseBasic houseBasic = AppHouseMapper.INSTANCE.recordHouseBasic(houseBasicInfo);
       // 通过小区id查询小区信息
      ZfCommunity community =  communityService.getCommunity(houseBasic.getCommunityId(),houseBasic.getProvinceCode(),houseBasic.getCityCode());
       houseBasic.setCreateTime(new Date());
       houseBasic.setRecordTool("mini");
       houseBasic.setStatus("unmaintain");
       houseBasic.setRegionCode(community.getRegionCode());
       houseBasic.setStreetCode(community.getStreetCode());
       houseBasic.setProvinceName(community.getProvinceName());
       houseBasic.setCityName(community.getCityName());
       houseBasic.setRegionName(community.getRegionName());
       houseBasic.setStreetName(community.getStreetName());
        return baseMapper.insert(houseBasic);
    }


    @Override
    public HouseDetailInfoDto getHouseDetailInfo(Long provCode, Long cityCode, Long basicId) {
        //1、获取图片信息
        List<AdminHousePicDto> pictures = pictureService.getPics(basicId, provCode, cityCode);
        //3、 房源基础信息，如户型，面积，朝向等
        HouseDetailAdminDto detailDto = baseMapper.getBasicInfo(basicId, provCode, cityCode);
        // 6 获取房源特色信息- 房源介绍
        HouseDescMo houseDesc = descMapper.getDes(basicId, provCode, cityCode);
        HouseDetailInfoDto detailInfoDto = new HouseDetailInfoDto();
        detailInfoDto.setDetailDto(detailDto);
        detailInfoDto.setHousePicDtos(pictures);
        detailInfoDto.setHouseDesc(houseDesc);

        return detailInfoDto;
    }

    @Override
    public HouseDetailInfoDto getHouseAndC(Long provCode, Long cityCode, Long basicId,  Long communityId,int type) {

        HouseDetailInfoDto detailInfoDto = new HouseDetailInfoDto();
        // 获取租房或者二手房源信息
            HouseRentInfoDto rent=null;
            HouseSaleInfoDto sale=null;
            // 小区基础信息
            CommunityNearByDto community = communityService.findommunityBasic(communityId );
            detailInfoDto.setCommunityBrandDto(community);
            // 获取经纪人信息
            List<AgentInfoDto> agentDtos = agentService.getAgentByCommunityId(communityId);
            log.info("房源详情经纪人信息:{}",agentDtos.size());
            detailInfoDto.setAgentDtos(agentDtos);
            if (type==1){
                rent = rentService.getHouseInfoRent(basicId, provCode, cityCode);
            } else if (type==2){
                sale = saleService.getHouseInfoSale(basicId, provCode, cityCode);
            }
            detailInfoDto.setRentInfoDto(rent);
            detailInfoDto.setSaleInfoDto(sale);
            // 获取同小区在租或者在售房源 前5条
            Page<HouseListMo> houseListMo =  this.houseBasic(provCode,cityCode,communityId, type,basicId,1,5);
            detailInfoDto.setHouseListMoPage(houseListMo);
            // 附近小区
            if (community!=null){
                String location =community.getLocation();
                double lat =Double.parseDouble( location.split(",")[1]);
                double lon =Double.parseDouble( location.split(",")[0]);
                //获取附近小区信息
                List<CommunityNearByDto> nearCommunityList=communityService.nearCommunityList(communityId,lat,lon,3000);
                detailInfoDto.setNearCommunity(nearCommunityList);
            }
        return  detailInfoDto;
    }

    @Override
    public Page<HouseListMo> houseBasic(Long provCode, Long cityCode, Long communityId, int type,Long houseId ,int current, int pageSize) {
        Page<HouseListMo> page = new Page<>(current, pageSize);
        List<HouseListMo> list = baseMapper.getHouseListByCommunity(provCode, cityCode, communityId, type,houseId, page);
        if (list != null && list.size() > 0) {
            for (HouseListMo mo : list
            ) {
                List<AdminHousePicDto> pics = pictureService.getPics(Long.parseLong(mo.getId()), provCode, cityCode, 1);
                if (pics.size() > 0) {
                    mo.setUrl(pics.get(0).getUrl());
                }
            }
        }
        page.setRecords(list);
        return page;
    }



}

